<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>编辑预收账款</title>
    <script type="text/javascript">
      Ext.onReady(function(){

        var advanceId = new Ext.form.Hidden({
          name: 'id',
          value: '<%=params[:id]%>'
        });
        
        var customerTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '<div style="float:left; width:70px">{number}</div>|{name}', '</div></tpl>');
        var customerStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/customer/getAllCustomers?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number',
              convert:function(value,record){
                if (value === ""){
                  return "未设置编号"
                }else{
                  return value;
                }
              }
            }, {
              name: 'name'
            }, {
              name: 'linkman'
            }, {
              name: 'phone'
            }, {
              name: 'address'
            }])
        });
        customerStore.load();
        
        var customer =  new Ext.form.ComboBox({
          fieldLabel: '客户',
          store: customerStore,
          valueField: 'id',
          displayField: 'name',
          hiddenName:'customer_id',
          allowBlank: false,
          //typeAhead: false,
          loadingText: '搜索中...',
          width:300,
          //pageSize: 10,
          //hideTrigger: false,
          tpl: customerTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          anchor: '95%',
          selectOnFocus:true
        });

        //汇入时间
        var actionDate = new Ext.form.DateField({
          fieldLabel: '汇入日期',
          name: 'action_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: false,
          blankText: "汇入日期必须选择",
          emptyText: '汇入日期...',
          anchor: '90%'
        });

        var amount = new Ext.form.NumberField({
          fieldLabel: '金额',
          name:"amount",
          allowBlank: false,
          allowDecimals: true,
          allowNegative: false,
          anchor: '90%'
        });

        //所有银行
        var bankTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '<div style="float:left; width:45px">{number}</div>|{name}', '</div></tpl>');
        var bankStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/bank/getAllBank"
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'name'
            }])
        });

        bankStore.load();

        var bank = new Ext.form.ComboBox({
          store: bankStore,
          fieldLabel: '开户行',
          allowBlank: false,
          blankText: "银行必须选择",
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'bank_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '90%',
          //pageSize: 25,
          //hideTrigger: false,
          tpl: bankTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          selectOnFocus:true
        });

        //账号
        var account = new Ext.form.TextField({
          fieldLabel: '账号',
          name: 'account',
          allowBlank: false,
          anchor: '95%'
        });

        var promotionTemplate = new Ext.XTemplate('<tpl for="."><div  class="search-item"><div style="float:left; width:150px">{name}</div><div>', '{begin_date}-{end_date}', '</div></div></tpl>');
        var promotionStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/promotion/getAllPromotions"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'name'
            }, {
              name: 'begin_date',
              type: 'date',
              dateFormat: 'Y/m/d'
            }, {
              name: 'end_date',
              type: 'date',
              dateFormat: 'Y/m/d'
            }])
        });

        promotionStore.load();

        var promotion =  new Ext.form.ComboBox({
          fieldLabel: '推广策略',
          store: promotionStore,
          valueField: 'id',
          displayField: 'name',
          hiddenName:'promotion_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          width:340,
          //pageSize: 10,
          //hideTrigger: false,
          tpl: promotionTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          anchor: '95%',
          selectOnFocus:true
        });

        var desc = new Ext.form.TextArea({
          fieldLabel: '描述',
          name: 'description',
          height:90,
          anchor: '95%'
        });

        var editAdvanceForm = new Ext.FormPanel({
          labelWidth: 55, // label settings here cascade unless overridden
          frame:true,
          url: '/advance_payment/update',
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'name'
            }, {
              name: 'action_date',
              type: 'date',
              dateFormat: 'Y/m/d'
            }, {
              name: 'bank_id'
            }, {
              name: 'account'
            }, {
              name: 'amount'
            }, {
              name: 'percent'
            }, {
              name: 'customer_id'
            }, {
              name: 'promotion_id'
            }, {
              name: 'description'
            }]),
          bodyStyle:'padding:5px 5px 0',
          width: 600,
          //height:300,
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .7,
                  layout: 'form',
                  border: false,
                  items: [customer]
                },{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [actionDate]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [bank]
                },{
                  columnWidth: .4,
                  layout: 'form',
                  border: false,
                  items: [account]
                },{
                  columnWidth: .3,
                  layout: 'form',
                  border: false,
                  items: [amount]
                }]
            },promotion,desc,advanceId],
          buttons: [{
              text: '提交',
              handler: function(){
                // check form value
                if (editAdvanceForm.form.isValid()) {
                  editAdvanceForm.form.submit({
                    method: 'GET',
                    success: function(result, response){
                      if (response.result.info == "成功") {
                        //添加成功后，隐藏窗口，并刷新Grid
                        editAdvanceWin.destroy();
                      }
                      else {
                        Ext.MessageBox.alert('消息', response.result.info);
                      }
                    },
                    failure: function(result, response){
                      Ext.MessageBox.alert('提示', result.responseText);
                    }
                  });
                }
                else {
                  Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                }
              }
            },{
              text: '取消',
              handler: function(){
                editAdvanceWin.destroy();
              }
            }]
        });
        //编辑前，把数据引入到表单中。
        // load form and assign value to fields
        editAdvanceForm.form.load({
          url: '/advance_payment/getAdvancePaymentById?id=<%=params[:id]%>',
          method : 'GET',
          waitMsg: '正在载入数据...',
          success: function(form, action){
          },
          failure: function(form, action){
            Ext.MessageBox.alert('提示信息', '信息加载失败');
          }
        });
            
        editAdvanceForm.render('edit_advance_form');
      })
    </script>
  </head>
  <body>
    <div id="edit_advance_form"></div>
  </body>
</html>
